#!/bin/bash
# 安装依赖
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install -y libffi-devel
# 下载python并安装
if [ ! -d Python-3.8.9  ];then
wget https://mirrors.huaweicloud.com/python/3.8.9/Python-3.8.9.tgz
tar -zxvf Python-3.8.9.tgz
fi
cd Python-3.8.9

echo "安装Python......"
# 检查、编译及安装
./configure -q
make -s && make install -s

if [ $? -eq 0 ];then
sleep 3
# 把python3 默认成为python的命令，python2变成python.bak
mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python3 /usr/bin/python

sleep 3
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip3 /usr/bin/pip

pip install --upgrade pip

# 修改yum指定的python版本，指定为2.7，yum默认只支持2.7
sed -i "1s/python/python2.7/g" /usr/libexec/urlgrabber-ext-down
sed -i "1s/python/python2.7/g" /usr/bin/yum
fi
echo "pip更换国内源......"
mkdir ~/.pip
cp ../pip.conf ~/.pip
pip config list
pip install --upgrade pip


echo "检查安装结果......"
yum check-update -q
if [ $? -eq 0 ];then
echo "yum使用正常！"
fi
pip config list && python -V
if [ $? -eq 0 ];then
echo "Python-3.8.9安装配置成功！"
fi
